/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.notify.web;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.notify.entity.NotiApp;
import com.thinkgem.jeesite.modules.notify.service.NotiAppService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 推送app管理Controller
 *
 * @author hf
 * @version 2018-12-17
 */
@Controller
@RequestMapping(value = "${adminPath}/notify/notiApp")
public class NotiAppController extends BaseController {

    @Autowired
    private NotiAppService notiAppService;

    @ModelAttribute
    public NotiApp get(@RequestParam(required = false) String id) {
        NotiApp entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = notiAppService.get(id);
        }
        if (entity == null) {
            entity = new NotiApp();
        }
        return entity;
    }

    @RequiresPermissions("notify:notiApp:view")
    @RequestMapping(value = {"list", ""})
    public String list(NotiApp notiApp, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<NotiApp> page = notiAppService.findPage(new Page<NotiApp>(request, response), notiApp);
        model.addAttribute("page", page);
        return "modules/notify/notiAppList";
    }

    @RequiresPermissions("notify:notiApp:view")
    @RequestMapping(value = "form")
    public String form(NotiApp notiApp, Model model) {
        model.addAttribute("notiApp", notiApp);
        return "modules/notify/notiAppForm";
    }

    @RequiresPermissions("notify:notiApp:edit")
    @RequestMapping(value = "save")
    public String save(NotiApp notiApp, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, notiApp)) {
            return form(notiApp, model);
        }
        notiAppService.save(notiApp);
        addMessage(redirectAttributes, "保存推送app管理成功");
        return "redirect:" + Global.getAdminPath() + "/notify/notiApp/?repage";
    }

    @RequiresPermissions("notify:notiApp:edit")
    @RequestMapping(value = "delete")
    public String delete(NotiApp notiApp, RedirectAttributes redirectAttributes) {
        notiAppService.delete(notiApp);
        addMessage(redirectAttributes, "删除推送app管理成功");
        return "redirect:" + Global.getAdminPath() + "/notify/notiApp/?repage";
    }

    @RequestMapping("/getAppid")
    @ResponseBody
    public List<String> getAppid() {
        return notiAppService.getAppid();
    }

}